﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AussieAuto.DataAccess;
using System.Data.Entity;
using AussieAuto.Controller;


namespace AussieAuto.TestHarness
{
    [TestClass]
    public class UnitTests
    {

       static AussieAutoEntityModel entityContext = new AussieAutoEntityModel();
        CustomerCarController CustomerCarController = new CustomerCarController(entityContext);
        RepairJobController BusinessLogicRepairJob = new RepairJobController(entityContext);
        InvoicePaymentController invoicePaymentController = new InvoicePaymentController(entityContext);

        public Customer createRandomTestCustomer()
        {
            Random rand = new Random();
            Customer customer = new Customer();
            customer.StreetAddress = rand.Next().ToString() + " streetName";
            customer.Suburb = "Suburb" + DateTime.Now.Millisecond.ToString();
            customer.PostCode = rand.Next(2000, 8000).ToString();
            customer.LastName = "Fonseka" + DateTime.Now.Millisecond.ToString();
            customer.FirstName = "Isuru" + DateTime.Now.Millisecond.ToString();
            customer.LookupStateId = 1;
    
            return customer;
        }

        public Car createRandomTestCar()
        {

            Random rand = new Random();
            Car car = new Car();
            car.Make = "Make" + DateTime.Now.Millisecond.ToString();
            car.Model = "Model" + DateTime.Now.Millisecond.ToString();
            car.Year = rand.Next(1900, DateTime.Now.Year);
            car.RegoNumber = "ABC" + rand.Next(100, 999);
            car.LookupRegoStateId = 1;
            return car;

        }

        public JobLine createRandomJobLine(RepairJob repairJob)
        {
            Random rand = new Random();
            JobLine jobline = new JobLine();
            jobline.JobLineText = "JOBLINE + " + DateTime.Now.Millisecond.ToString();
            jobline.Qty = rand.Next(0, 999);
            jobline.UnitPrice = rand.Next(1, 2000);
            jobline.RepairJob = repairJob;

            return jobline;
        }





        [TestMethod]
        public void CreateCustomerCarOwn()
        {
            Customer customer = createRandomTestCustomer();
            CustomerCarController.saveCustomer(customer);

            Car car = createRandomTestCar();
            CustomerCarController.saveCar(car);
            Assert.IsNotNull(CustomerCarController.saveOwn(customer, car));



        }




        [TestMethod]
        public void CreateCustomer()
        {

            Assert.IsTrue(CustomerCarController.saveCustomer(createRandomTestCustomer()));
        }


        public void GetCustomer()
        {
            // Assert.IsNotNull(CustomerCarController.getcu
        }

        [TestMethod]
        public void CreateCar()
        {
            Assert.IsTrue(CustomerCarController.saveCar(createRandomTestCar()));
        }

     
        public int CreateRepairJob()
        {
            Random rand = new Random();


            try
            {

                Car car = new Car();
                car = createRandomTestCar();
                CustomerCarController.saveCar(car);

                Customer customer = new Customer();
                customer = createRandomTestCustomer();
                RepairJob repairJob = new RepairJob();
                repairJob.Own = new Own();
                repairJob.Own.Car = car;
                repairJob.Own.Customer = customer;
                repairJob.LookupRepairJobTypeId = 2;
                repairJob.BookingDate = DateTime.Now;

                BusinessLogicRepairJob.saveRepairJob(repairJob);

                AussieAutoEntityModel EntityContext = new AussieAutoEntityModel();
                //  Car car = new Car();
                car = createRandomTestCar();
                EntityContext.AddToCars(car);



                EntityContext.SaveChanges();
                return repairJob.RepairJobId;
                //if no errors return as successful
                // return true;
            }
            catch (Exception ex)
            {
                return 0;
                //TO DO: INSERT LOGGING 
                //return false;
                // throw ex;

            }

        }

   
        public int CreateNewRepairJobWithJobLines()
        {
            Random rand = new Random();


            try
            {

                Car car = new Car();
                car = createRandomTestCar();
           //     CustomerCarController.saveCar(car);
                Customer customer = new Customer();
                customer = createRandomTestCustomer();
                RepairJob repairJob = new RepairJob();
                repairJob.Own = new Own();
                repairJob.Own.Car = car;
                repairJob.Own.Customer = customer;
                repairJob.ProblemDesc = "TEST";
                repairJob.LookupRepairJobTypeId = 2;
                repairJob.BookingDate = DateTime.Now;

                JobLine jobline = createRandomJobLine(repairJob);

                repairJob.JobLines.Add(jobline);

                BusinessLogicRepairJob.saveRepairJob(repairJob);
              //  BusinessLogicRepairJob.saveJobLines(repairJob);

                Assert.IsTrue(jobline.JobLineId > 0);


              

                return repairJob.RepairJobId;



                //if no errors return as successful
                // return true;
            }
            catch (Exception ex)
            {
                return 0;
                //TO DO: INSERT LOGGING 
                //return false;
                throw ex;

            }

        }

        //[TestMethod]
        //public void addandDeleteRepairJob()
        //{
        // int id=   CreateRepairJob();
        // RepairJob repairJob = BusinessLogicRepairJob.EntityContext.RepairJobs.Select(p => p.RepairJobId == id) as RepairJob;
        //    Assert.IsTrue(BusinessLogicRepairJob.deleteRepairJob(repairJob));

        
        //}


        [TestMethod]
        public void addandDeleteRepairJobincludingJobLines()
        {

  Random rand = new Random();


     

                Car car = new Car();
                car = createRandomTestCar();
           //     CustomerCarController.saveCar(car);

                Customer customer = new Customer();
                customer = createRandomTestCustomer();
                RepairJob repairJob = new RepairJob();
                repairJob.Own = new Own();
                repairJob.Own.Car = car;
                repairJob.Own.Customer = customer;
                repairJob.ProblemDesc = "TEST";
                repairJob.LookupRepairJobTypeId = 2;
                repairJob.BookingDate = DateTime.Now;

                JobLine jobline = createRandomJobLine(repairJob);

                repairJob.JobLines.Add(jobline);

                BusinessLogicRepairJob.saveRepairJob(repairJob);
                BusinessLogicRepairJob.saveJobLines(repairJob);  

              
            Assert.IsTrue(BusinessLogicRepairJob.deleteRepairJob(repairJob));



        }

        [TestMethod]
        public void addJobTodayAndCheckBookingList()
        {

            CreateNewRepairJobWithJobLines();

         Assert.IsTrue(BusinessLogicRepairJob.getrepairJobsByDate(DateTime.Now).Count >0);
        
        }

   //     [TestMethod]
        public RepairJob AddNewJoblinesToExistingRepiarJob()
        {
            Random rand = new Random();
            try
            {
                var repairJob = BusinessLogicRepairJob.EntityContext.RepairJobs.FirstOrDefault();
              

                JobLine jobline1 = createRandomJobLine(repairJob);
                JobLine jobline2= createRandomJobLine(repairJob);
                JobLine jobline3 = createRandomJobLine(repairJob);
                repairJob.JobLines.Add(jobline1);
                repairJob.JobLines.Add(jobline2);
                repairJob.JobLines.Add(jobline3);

      
                BusinessLogicRepairJob.saveJobLines(repairJob);          
                    
                return repairJob;
                //if no errors return as successful
                // return true;
            }
            catch (Exception ex)
            {

                //TO DO: INSERT LOGGING 
                //return false;
                throw ex;

            }

        }


        [TestMethod]
        public void CreateInoice()
        {

           // RepairJob repairJob = AddNewJoblinesToExistingRepiarJob(); 
            Invoice invoice = new Invoice();
            invoice.RepairJob = new RepairJob();
            invoice.RepairJob = invoicePaymentController.EntityContext.RepairJobs.Where(p => p.RepairJobId == 1).FirstOrDefault();

//invoicPeaymentController.EntityContext.AddToInvoices(invoice);
       //     invoicePaymentController.EntityContext.SaveChanges();

            Assert.IsTrue(invoicePaymentController.createNewInvoiceForRepairJob(invoice));
        }

        [TestMethod]
        public void CreateInoiceWithDiscount()
        {

            // RepairJob repairJob = AddNewJoblinesToExistingRepiarJob(); 
            Invoice invoice = new Invoice();
            invoice.RepairJob = new RepairJob();
            invoice.RepairJob = invoicePaymentController.EntityContext.RepairJobs.Where(p => p.RepairJobId == 26).FirstOrDefault();
            invoice.LookupDiscountId = 2;
            //invoicePaymentController.EntityContext.AddToInvoices(invoice);
            //     invoicePaymentController.EntityContext.SaveChanges();

            Assert.IsTrue(invoicePaymentController.createNewInvoiceForRepairJob(invoice));
        }





    }
}
